package com.yiqi.zhiyuan.feature.order.view.adapter;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.utils.T;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.vo.CarVO;
import com.yiqi.zhiyuan.feature.home.view.adapter.CarTypeAdapter;
import com.yiqi.zhiyuan.feature.home.view.adapter.ListenOrderAdapter;
import com.yiqi.zhiyuan.framework.DataServer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author LIJUWEN
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/25 15:22
 */
public class CommonDialog extends Dialog implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rv_dialog_type)
    RecyclerView mRvDialogType;
    @BindView(R.id.rv_dialog_listen)
    RecyclerView mRvDialogListen;
    private Context context;

    CarTypeAdapter mCarTypeAdapter;//车型适配器
    ListenOrderAdapter mListenOrderAdapter;//接听订单适配器

    private int mCarPosition=-1;

    private List<CarVO> mCarVOS;

    private MyClickListenerInterface listenerInterface;
    private LayoutInflater layoutInflater;
    private int mListenType=0;
    public CommonDialog(Context context,List<CarVO> homeVo) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context = context;
        this.mCarVOS=homeVo;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        View view = layoutInflater.inflate(R.layout.dialog_car_setting, null);
        ButterKnife.bind(this, view);
        setContentView(view);

        initRecycleView();
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);

    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);

    }

    public void setListener(MyClickListenerInterface listener) {
        this.listenerInterface = listener;
    }

    @OnClick({R.id.btn_dialog_listen_close, R.id.btn_dialog_listen_complete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_dialog_listen_close:
                dismiss();
                break;
            case R.id.btn_dialog_listen_complete:
                if (listenerInterface != null) {
                    listenerInterface.doConfirm(mCarVOS.get(mCarPosition),mListenType);
                } else {
                    dismiss();
                }

                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        mCarPosition=position;
        mCarTypeAdapter.checkState(position);
    }


    public interface MyClickListenerInterface {
        void doConfirm(CarVO carVO,int listenType);
    }


    private void initRecycleView() {

        mRvDialogType.setLayoutManager(new GridLayoutManager(context, 3));
        mCarTypeAdapter = new CarTypeAdapter();
        mCarTypeAdapter.bindToRecyclerView(mRvDialogType);
        mCarTypeAdapter.setOnItemClickListener(this);
        List<CarVO> carVOS=new ArrayList<>();
        for (int i = 0; i <mCarVOS.size() ; i++) {
            if (mCarVOS.get(i).getIsPass()==2){
                carVOS.add(mCarVOS.get(i));
            }
        }
        setCarData(carVOS);
        mRvDialogListen.setLayoutManager(new GridLayoutManager(context, 3));
        mListenOrderAdapter = new ListenOrderAdapter();
        mListenOrderAdapter.bindToRecyclerView(mRvDialogListen);
        mListenOrderAdapter.setNewData(DataServer.getListenType());
        mListenOrderAdapter.setOnItemClickListener(click);
        mListenOrderAdapter.checkState(mListenType);
    }

    private BaseQuickAdapter.OnItemClickListener click = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                mListenType=position;
                mListenOrderAdapter.checkState(mListenType);


        }
    };

    public void setCarData(List<CarVO> carVOS) {
        if (mCarTypeAdapter != null) {
            mCarTypeAdapter.setNewData(carVOS);
            for (int i = 0; i <carVOS.size() ; i++) {
                if (carVOS.get(i).getIsUse()==1){
                    mCarPosition=i;
                    mCarTypeAdapter.checkState(mCarPosition);
                }
            }
        }
    }

    public void setCarListen(int listenPos) {
        this.mListenType = listenPos;
        if (mListenOrderAdapter != null) {
            mListenOrderAdapter.checkState(mListenType);
        }
    }
}
